package com.inray.nbs.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.inray.nbs.bean.Userinfo;
import com.inray.nbs.service.UserService;
import com.inray.nbs.service.UserSetService;
import com.inray.nbs.utils.MD5;
/**
 * 登陆控制器
 * UserController
 * @author Ming Yang
 */
@Controller
@RequestMapping(value = "/user")
public class UserController {

	@Resource
	private UserService userService;
	@Resource
	private UserSetService setService;
	/**
	 * 
	 * login(验证用户名 密码)
	 * @param request
	 * @param user
	 * @return 
	 * String
	 * @exception 
	 * @since  1.0.0
	 */
	@RequestMapping(value = "/login.do", method=RequestMethod.POST  )
	@ResponseBody
	public String login(HttpServletRequest request, Userinfo user) {
		Userinfo u1=user;
		JSONObject json=new JSONObject();
			user.setLoginpassword(MD5.getPsw(user.getLoginpassword()));//加密
			Userinfo u = userService.selectByLogin(user);
			if (u != null){
				u.setIp(user.getIp());
				//验证成功放入session
				int i = setService.SaveIpAddr(u);
				if(i>0){
					System.out.println("用户登录记录成功");
				}else{ 
					System.out.println("用户登录记录失败");
				}
				request.getSession().setAttribute("user", u);
				request.getSession().setAttribute("userid", u.getUserid());
				json.put("realname", u.getUserrealname());
				json.put("success", "success");
			}
			else {
				json.put("success", "fail");
			}
			return json.toString();
	}
	/**
	 * 查看已登录用户信息
	 * @param request
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "/selectLoginEdUser.do", method=RequestMethod.POST  )
	@ResponseBody
	public String selectLoginEdUser(HttpServletRequest request) {
		JSONObject json=new JSONObject();
		Userinfo user = (Userinfo)request.getSession().getAttribute("user");
		json.put("user", user);
		return json.toString();
	}
	/**
	 * 用户退出
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/logout.do")
	public String logout(HttpServletRequest request){
		Userinfo sessionUser = (Userinfo)request.getSession().getAttribute("user");
		if(sessionUser!=null){
			request.getSession(false).removeAttribute("user");
		}
		return "redirect:/login.jsp";
	}
	/**
	 * home(登陆成功跳转控制器)
	 * @param request
	 * @return 
	 * String
	 * @exception 
	 * @since  1.0.0
	 */
	@RequestMapping(value="/home.do")
	public String home(HttpServletRequest request){
		//Userinfo user = (Userinfo) request.getSession().getAttribute("user");//得到当前登录的用户信息
		return "index";
	}
	/**
	 * 添加cookie
	 * @param response
	 * @param value
	 * @return
	 */
	@RequestMapping(value="/setCookie.do",method=RequestMethod.POST)
	@ResponseBody
	public String setCookie(HttpServletResponse response,String name,String value){
		JSONObject json=new JSONObject();
		try{
			  	Cookie cookie = new Cookie(name,value);
			    cookie.setPath("/");
			    cookie.setMaxAge(3600*24*365*5);
			    response.addCookie(cookie);
				json.put("success", "success");
		} catch (NumberFormatException e) {
			e.printStackTrace();
			json.put("success", "no");
		}
		return json.toString();
	}

	@RequestMapping(value="/getCookie.do",method=RequestMethod.POST)
	@ResponseBody
	public String getCookie(HttpServletRequest request,String name){
		JSONObject json=new JSONObject();
		System.out.println("查找网站整体风格："+name);
		try{
			 Map<String,Cookie> cookieMap = ReadCookieMap(request);
			    if(cookieMap.containsKey(name)){
			        Cookie cookie = (Cookie)cookieMap.get(name);
			        json.put("value", cookie.getValue());
			    }else{
			    	json.put("value", null);
			    }   
				
		} catch (NumberFormatException e) {
			e.printStackTrace();
			json.put("value", null);
		}
		return json.toString();
	}
	private static Map<String,Cookie> ReadCookieMap(HttpServletRequest request){  
	    Map<String,Cookie> cookieMap = new HashMap<String,Cookie>();
	    Cookie[] cookies = request.getCookies();
	    if(null!=cookies){
	        for(Cookie cookie : cookies){
	            cookieMap.put(cookie.getName(), cookie);
	        }
	    }
	    return cookieMap;
	}
}
